import { defineStore } from "pinia";
import { JxtechToken } from "@/stores/interface/token";
import piniaPersistConfig from "@/stores/helper/persist";

export const useTokenStore = defineStore({
  id: "jxtech-token",
  state: (): JxtechToken => ({
    /**用户token */
    access_token: null,
    /**刷新token */
    refresh_token: null,
    short_token: null,
    client_id: null,
    expires_in: null,
    locale: null,
    scope: null,
    token_type: null,
    username: null
  }),
  getters: {},
  actions: {
    /**设置token信息 */
    setToken(responseData: JxtechToken) {
      this.access_token = responseData.access_token;
      this.refresh_token = responseData.refresh_token;
      this.short_token = responseData.short_token;
      this.client_id = responseData.client_id;
      this.expires_in = responseData.expires_in;
      this.locale = responseData.locale;
      this.scope = responseData.scope;
      this.token_type = responseData.token_type;
      this.username = responseData.username;
    },
    /**清空token信息 */
    clearToken() {
      this.$reset();
    }
  },
  persist: piniaPersistConfig("jxtech-token")
});
